package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AreTwoCharArrayEquivalent {
	public static void main(String[] argv) {
		char[] charArr1 = new char[]{'t', 'a', 'h', '1', '9', '8', '6',
				'.', 'c', 'o', 'm'};
		char[] charArr2 = new char[]{'t', 'a', 'h', '1', '9', '8', '6',
				'.', 'c', 'o', 'm'};
		System.out.println(isEquivalent(charArr1, charArr2));
	}

	public static boolean isEquivalent(char[] charArr1, char[] charArr2) {
		if (charArr1.length != charArr2.length) {
			return false;
		}
		int sum1 = 0;
		int sum2 = 0;
		for (int index = 0; index < charArr1.length; index++) {
			sum1 += charArr1[index];
			sum2 += charArr2[index];
		}
		if (sum1 != sum2) {
			return false;
		}
		List<Character> charList1 = toList(charArr1);
		List<Character> charList2 = toList(charArr2);
		for (Character charValue : charList1) {
			charList2.remove(charValue);
		}
		return charList2.isEmpty();
	}

	private static List<Character> toList(char[] charArr) {
		assert charArr != null;
		List<Character> charList = new ArrayList<Character>();
		for (char ch : charArr) {
			charList.add(ch);
		}
		return charList;
	}
}
